/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rdb.sbean;

import com.rdb.entities.*;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Bich Tran Van
 */
@Stateless
public class MessagesFacade extends AbstractFacade<Messages> {
    @PersistenceContext(unitName = "ResultDeclarationBoard-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public MessagesFacade() {
        super(Messages.class);
    }
    
    public List<Messages> getMessages(int userId, int userType){
        String query = "SELECT m FROM Messages m WHERE 1 = 1 ";
        if(userType == 1){
            query = query + " AND m.userId.userId = :userId";
        }else{
            query = query + " AND m.userId is null";
        }
        
        query = query + " order by m.id DESC";
        javax.persistence.Query q = getEntityManager().createQuery(query);
        if(userType == 1){
            q.setParameter("userId", userId);
        }
        if(q.getResultList().isEmpty()){
            return null;
        }
        return q.getResultList();
    }
    
}
